package com.sws.orderMS.command.impl;

import java.util.List;

import com.sws.orderMS.OrderMSDao;
import com.sws.orderMS.command.UserCommand;
import com.sws.orderMS.pojo.Product;
import com.sws.orderMS.pojo.User;
import com.sws.orderMS.tool.Tool;

public class ProductManageCommand extends UserCommand {

	public ProductManageCommand(User userProfile) {
		super(userProfile);
	}

	@Override
	public void execute() {
		String userid =Tool.askUserInputNotNull("请输入你需要的操作ADD|DELETE|UPDATE|LIST");
		if(!"ADD|DELETE|UPDATE|LIST".contains(userid.toUpperCase())){
			Tool.printMsg("你输入的命令不符合，请重新输入");
			return;
		}
		OrderMSDao dao = new OrderMSDao();
		List<Product> list = null;
		Product product = new Product();
		if("ADD".equalsIgnoreCase(userid)){
			List<String> productType = dao.getAllProductType();
			String selectMenu = "";
			for (String str: productType) {
				selectMenu = selectMenu.concat(str).concat("|");
			}
			selectMenu = selectMenu.concat("new");
			String type = Tool.askUserInputNotNull("选择添加的商品类型:" + selectMenu);
			if (!selectMenu.contains(type.toLowerCase())) {
				Tool.printMsg("你输入的商品类型不符合，请重新输入");
				return;
			}
			if("new".equalsIgnoreCase(type)){
				String code = Tool.askUserInputNotNull("请输入你要商品类型代号");
				String name = Tool.askUserInputNotNull("请输入新的商品类型名称");
				String description = Tool.askUserInputNotNull("请简要的说明商品类型");
				dao.insertProductType(code, name, description);
				type = name;
			}
			int productTypeId = dao.getProductTypeId(type);
			System.out.println("你要选择的商品类型的ID是"+productTypeId);
			String code =Tool.askUserInputNotNull("请输入商品代号");
			String name =Tool.askUserInputNotNull("请输入商品名称");
			String price =Tool.askUserInputNotNull("请输入商品单价");
			String qty =Tool.askUserInputNotNull("请输入商品数量");
			String description =Tool.askUserInputNotNull("请输入商品简介");
			int tbl_product_unit_id =productTypeId;
			product.setCode(code);
			product.setName(name);
			product.setPrice(Double.parseDouble(price));
			product.setType(type);
			product.setQty(Integer.parseInt(qty));
			product.setDescription(description);
			dao.insertProduct(product, productTypeId);
			return;
		}else if ("DELETE".equalsIgnoreCase(userid)){
			String idStr =Tool.askUserInputNotNull("你要删除商品的ID");
			int id = Integer.parseInt(idStr);
			dao.delectProduct(id);
			Tool.printMsg("删除成功");
			return;
		}else if("UPDATE".equalsIgnoreCase(userid)){
			String updateId = Tool.askUserInputNotNull("请输入要改变的商品的ID");
			int id = Integer.valueOf(updateId);
			product.setId(id);
			List<String> colunm =dao.getProductCollunm();
			String colunmName = "";
			for(String str:colunm){
				colunmName =colunmName.concat(str).concat("|");
			}
			String name =Tool.askUserInputNotNull("请输入商品名称");
			String price =Tool.askUserInputNotNull("请输入商品单价");
			String qty =Tool.askUserInputNotNull("请输入商品数量");
			String description =Tool.askUserInputNotNull("请输入商品简介");
			product.setName(name);
			product.setPrice(Double.parseDouble(price));
			product.setQty(Integer.parseInt(qty));
			product.setDescription(description);
			
			dao.updateProduct(product);
			return;
		}else if ("LIST".equalsIgnoreCase(userid)){
			ListProductCommand listCmd = new ListProductCommand(userProfile);
			listCmd.execute();
		}
		
	}
	}


